package com.LearnInWorld.video.service.impl;

import com.LearnInWorld.video.mapper.VideoMapper;
import com.LearnInWorld.video.pojo.Video;
import com.LearnInWorld.video.service.VideoServoce;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class VideoServoceImpl implements VideoServoce {

    @Autowired
    private VideoMapper videoMapper;

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Video> findALl() {
        List<Video> videos = videoMapper.selectAll();
        return videos;
    }

    /**
     * 根据ID查询video
     *
     * @param videoId
     * @return
     */
    @Override
    public Video findById(String videoId) {
        Video video = videoMapper.selectByPrimaryKey(videoId);
        return video;
    }

    /**
     * 根绝分类ID查询Video
     *
     * @param CategoryId
     * @return
     */
    @Override
    public List<Video> findByCategoryId(Integer CategoryId) {
        return null;
    }

    /**
     * 根绝老师名称查询
     *
     * @param teacherName
     * @return
     */
    @Override
    public List<Video> findByTeacherName(String teacherName) {
        return null;
    }

    /**
     * 条件查询
     *
     * @param condition
     * @return
     */
    @Override
    public List<Video> findByCondition(Map<String, Object> condition) {
        Example example = createExample(condition);
        return videoMapper.selectByExample(condition);
    }

    /**
     * 分页搜索
     *
     * @param page 页码
     * @param size 页大小
     * @return
     */
    @Override
    public Page<Video> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return null;
    }

    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @Override
    public Page<Video> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        return (Page<Video>) videoMapper.selectByExample(example);
    }

    /**
     * 新增
     *
     * @param video
     */
    @Override
    public void add(Video video) {
        videoMapper.insertSelective(video);
    }

    /**
     * 修改
     *
     * @param video
     */
    @Override
    public void update(Video video) {
        videoMapper.updateByPrimaryKeySelective(video);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        videoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    public Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Video.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 视频id
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 视频名称
            if (searchMap.get("videoName") != null && !"".equals(searchMap.get("videoName"))) {
                criteria.andLike("videoName", "%" + searchMap.get("videoName") + "%");
            }
            // 视频数量
            if (searchMap.get("num") != null && !"".equals(searchMap.get("num"))) {
                criteria.andLike("num", "%" + searchMap.get("num") + "%");
            }
            // 视频图片
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
                criteria.andLike("image", "%" + searchMap.get("image") + "%");
            }
            // 视频图片列表
            if (searchMap.get("images") != null && !"".equals(searchMap.get("images"))) {
                criteria.andEqualTo("images", searchMap.get("images"));
            }
            // 视频介绍
            if (searchMap.get("videoInfo") != null && !"".equals(searchMap.get("videoInfo"))) {
                criteria.andLike("videoInfo", "%" + searchMap.get("videoInfo") + "%");
            }
            // 上传日期
            if (searchMap.get("timetableUpTime") != null && !"".equals(searchMap.get("timetableUpTime"))) {
                criteria.andLike("timetableUpTime", "%" + searchMap.get("timetableUpTime") + "%");
            }
            // 视频类型
            if (searchMap.get("timetableType") != null && !"".equals(searchMap.get("timetableType"))) {
                criteria.andLike("timetableType", "%" + searchMap.get("timetableType") + "%");
            }
            // 播放次数
            if (searchMap.get("playSum") != null && !"".equals(searchMap.get("playSum"))) {
                criteria.andEqualTo("playSum", searchMap.get("playSum"));
            }

            // 视频url
            if (searchMap.get("timetableUrl") != null) {
                criteria.andEqualTo("timetableUrl", searchMap.get("timetableUrl"));
            }
            // 视频大小
            if (searchMap.get("size") != null) {
                criteria.andEqualTo("size", searchMap.get("size"));
            }
            // 评论数
            if (searchMap.get("commentMun") != null) {
                criteria.andEqualTo("commentMun", searchMap.get("commentMun"));
            }
        }
        return example;
    }
}
